package com.ppd.generator.wrapper;/**
 * Created by abel.lin on 2017/2/27.
 */

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * @author abel.lin
 * @date_time 2017/2/27 14:26
 * @description
 */
public class GenerateUtils {
    private static final Logger log = Logger.getLogger(GenerateUtils.class);
    public static void geneFile(String content, String fileName, String path){
        try {
            if(StringUtils.isBlank(content) || StringUtils.isBlank(fileName) || StringUtils.isBlank(path)){
                log.info("generate fail...");
                log.info("content="+content+", fileName="+fileName+", path="+path);
                return;
            }
            File file = new File(path);
            if(!file.exists()) {
                file.mkdirs();
            }
            String outputPath = path+fileName;

            FileOutputStream fos = new FileOutputStream(outputPath);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            osw.write(content);
            osw.flush();
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 首字母大写
     *
     * @param srcStr
     * @return
     */
    public static String firstCharToUpper(String srcStr) {
        return srcStr.substring(0, 1).toUpperCase() + srcStr.substring(1);
    }
    /**
     * 替换字符串并让它的下一个字母为大写
     * @param srcStr
     * @param org
     * @param ob
     * @return
     */
    public static String underlineToUpper(String srcStr,String org,String ob){
        String newString = "";
        int first=0;
        while(srcStr.indexOf(org)!=-1)
        {
            first=srcStr.indexOf(org);
            if(first!=srcStr.length())
            {
                newString=newString+srcStr.substring(0,first)+ob;
                srcStr=srcStr.substring(first+org.length(),srcStr.length());
                srcStr=firstCharToUpper(srcStr);
            }
        }
        newString=newString+srcStr;
        return newString;
    }

    /**
     * 功能：获得列的数据类型
     * @param sqlType
     * @return
     */
    public static String sqlType2JavaType(String sqlType) {
        if(sqlType.equalsIgnoreCase("bit")){
            return "Boolean";
        }else if(sqlType.equalsIgnoreCase("smallint")){
            return "Short";
        }else if(sqlType.equalsIgnoreCase("tinyint") || sqlType.equalsIgnoreCase("int") || sqlType.equalsIgnoreCase("INT UNSIGNED")){
            return "Integer";
        }else if(sqlType.equalsIgnoreCase("bigint")){
            return "Long";
        }else if(sqlType.equalsIgnoreCase("float")){
            return "Float";
        }else if(sqlType.equalsIgnoreCase("decimal")){
            return "BigDecimal";
        }else if(sqlType.equalsIgnoreCase("numeric")
                || sqlType.equalsIgnoreCase("double")
                || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")
                || sqlType.equalsIgnoreCase("smallmoney")){
            return "Double";
        }else if(sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char")
                || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")
                || sqlType.equalsIgnoreCase("text")){
            return "String";
        }else if(sqlType.equalsIgnoreCase("date") || sqlType.equalsIgnoreCase("datetime")
                || sqlType.equalsIgnoreCase("timestamp")){
            return "Date";
        }else if(sqlType.equalsIgnoreCase("image")){
            return "Blod";
        }else{
            return "String";
        }
    }

    public static enum GenType{

        MODEL("model"),
        CRITERIA("criteria"),
        MAPPER("mapper"),
        SERVICE("service"),
        DOMAIN("domain"),
        CONTROLLER("controller");

        public final String code;

        private GenType(String code){
            this.code = code;
        }

        public String getCode(){
            return code;
        }
    }
}
